home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-03 | 17.5 KB | 569 lines | [TEXT/MPS ] |
- // UScripting.h
- // Copyright © 1984-96 by Apple Computer, Inc. All rights reserved.
-
- #ifndef __USCRIPTING__
- #define __USCRIPTING__
-
- // MacApp
-
- #ifndef __UAPPLEEVENTS__
- #include "UAppleEvents.h"
- #endif
-
- #ifndef __UCLASSDESC__
- #include "UClassDesc.h"
- #endif
-
- #ifndef __ULIST__
- #include "UList.h"
- #endif
-
- #ifndef __UOBJECT__
- #include "UObject.h"
- #endif
-
- #ifndef __USCRIPTABLEOBJECT__
- #include "UScriptableObject.h"
- #endif
-
- // Toolbox
-
-
- //----------------------------------------------------------------------------------------
- // Forward and external class declarations.
- //----------------------------------------------------------------------------------------
-
- class TCommand;
- class TEventList;
-
- //----------------------------------------------------------------------------------------
- // Structures
- //----------------------------------------------------------------------------------------
-
- struct MAEventTableRec // format of an entry in our dispatch table resource (type 'aedt')
- {
- OSType theClass;
- OSType theID;
- long theValue;
- };
- //----------------------------------------------------------------------------------------
- // TOSADispatcher
- //----------------------------------------------------------------------------------------
-
- class TOSADispatcher : public TObject
- {
- MA_DECLARE_CLASS;
-
- // This object manages the resolution and dispatching of incoming and outgoing
- // object model apple events.
-
- public:
-
- static TOSADispatcher* fgDispatcher;
-
- //------------------------------------------------------------------------------------
- // Initialization: creates one and one instance of the scripting dispatcher
- //------------------------------------------------------------------------------------
-
- static void InitUScripting(MA_ClassReference classDesc);
- // Initialize the scripting unit. Call this function to create a global
- // instance of TScriptEventDispatcher or one of its subclasses, and to
- // install events supported in Apple Event dispatch table resources.
-
- protected:
-
- TOSADispatcher();
- // Creation should be done only through InitUScripting.
-
- void IOSADispatcher();
- // Initialization should be done only through InitUScripting.
-
- public:
-
- virtual ~TOSADispatcher();
-
- inline void SetDefaultTarget(MDefaultScriptableObject* defaultTarget);
-
- inline MDefaultScriptableObject* GetDefaultTarget();
-
- inline long GetDispatchLevel() const { return fDispatchLevel; }
-
- inline void SetAEResolveFlags(const short newFlags);
-
- virtual MScriptableObject* ResolveObjectSpecifier(CAEDesc& objectDesc);
-
- virtual CommandNumber GetAppleEventCommandNumber(AEEventClass eventClass, AEEventID eventID);
-
- virtual TList* GetTemporaryTokens(void);
- // return the current list of temporary tokens
-
- virtual void AddTemporaryToken(TObject* tempToken);
- // Add the tempToken to a list whose contents will be disposed of after the
- // Apple Event has been handled.
-
- virtual void FreeTemporaryTokens();
- // Frees all the objects in the temp token disposal list.
-
- virtual void PostPendingReplyCommand(TCommand* command);
- // add a client command that is waiting for a reply to a local command
- // queue.
-
- #if qAttachable
- virtual void ScriptAttached();
- // Call when you have an attached OSA script that wants to receive
- // pre-dispatched Apple Events.
-
- virtual void ScriptDetached();
- // Call when you have an OSA script is detached and no longer wants
- // to receive pre-dispatched Apple Events.
- #endif
-
- protected:
-
- virtual void InstallDispatchHandlers();
-
- #if qAttachable
- virtual void InstallPreDispatchHandler();
-
- virtual void RemovePreDispatchHandler();
- #endif
-
- virtual void InstallObjectCallbacks();
-
- //------------------------------------------------------------------------------------
- // Dispatch Methods:
- //------------------------------------------------------------------------------------
-
- #if qAttachable
- virtual OSErr PreDispatchHandler(CommandNumber aCommandNumber, TAppleEvent* message,
- TAppleEvent* reply);
- #endif
-
- virtual OSErr DispatchHandler(CommandNumber aCommandNumber, TAppleEvent* message,
- TAppleEvent* reply);
-
- virtual void ReportDispatchError(AppleEvent* message, AppleEvent* reply,
- OSErr dispatchErr, long dispatchErrMessage);
-
- virtual MScriptableObject* GetTargetObject(CommandNumber aCommandNumber,
- TAppleEvent* appleEvent);
-
- //------------------------------------------------------------------------------------
- // Object Support Library (OSL) Callbacks :
- //------------------------------------------------------------------------------------
-
- virtual void ObjectAccessor(DescType desiredClass,
- CAEDesc& container,
- DescType containerClass,
- DescType form,
- CAEDesc& selectionData,
- CAEDesc& value,
- long accessorRefcon);
-
- virtual long CountObjects(DescType desiredType,
- DescType containerClass,
- CAEDesc& container);
-
- virtual Boolean CompareObjects(DescType whichOperation,
- CAEDesc& targetDesc,
- CAEDesc& thingToCompare);
-
- virtual void DisposeToken(CAEDesc& unneededToken);
-
- virtual void GetMarkToken(CAEDesc& theContainerToken,
- DescType containerClass,
- CAEDesc& result);
-
- virtual void MarkObject(CAEDesc& theToken,
- CAEDesc& markingListToken,
- long index);
-
-
- virtual void AdjustMarks(long newStart,
- long newStop,
- CAEDesc& markToken);
-
- virtual void MatchReplyToPending(TAppleEvent* reply);
- // match the supplied reply with a TClientCommand in the fPendingReplyList.
-
- //------------------------------------------------------------------------------------
- // Callback glue: glues static callbacks to matching virtual member functions.
- //------------------------------------------------------------------------------------
-
- #if qAttachable
- static pascal OSErr PreDispatchHandlerGlue(AppleEvent* message,
- AppleEvent* reply,
- long info);
- #endif
-
- static pascal OSErr DispatchHandlerGlue(AppleEvent* message,
- AppleEvent* reply,
- long info);
-
- static pascal OSErr ObjectAccessorGlue(DescType desiredClass,
- AEDesc* container,
- DescType containerClass,
- DescType form,
- AEDesc* selectionData,
- AEDesc* value,
- long accessorRefcon);
-
- static pascal OSErr CountObjectsGlue(DescType desiredType,
- DescType containerClass,
- AEDesc* container,
- long* result);
-
- static pascal OSErr CompareObjectsGlue(DescType whichOperation,
- AEDesc* obj1,
- AEDesc* obj2,
- Boolean* result);
-
- static pascal OSErr DisposeTokenGlue(AEDesc* unneededToken);
-
- static pascal OSErr GetMarkTokenGlue(AEDesc* containerToken,
- DescType containerClass,
- AEDesc* result);
-
- static pascal OSErr MarkObjectGlue(AEDesc* dToken,
- AEDesc* markToken,
- long index);
-
- static pascal OSErr AdjustMarksGlue(long newStart,
- long newStop,
- AEDesc* markToken);
-
- protected:
-
- MDefaultScriptableObject* fDefaultTarget; // When no other target is specified.
-
- TList* fTemporaryTokens; // token objects to be thrown out after
- // an event has been handled.
-
- long fDispatchLevel;
-
- short fAEResolveFlags; // callback flags passed to AEResolve
-
- TEventList* fPendingReplyList; // Commands that are waiting to be posted
- // pending the receipt of their reply.
- // Commands that are sent with QueueReply
- // end up in this queue
-
- AEEventHandlerUPP fDispatchUPP;
- #if qAttachable
- long fAttachedScripts; // Count of the number of attached
- // scripts expecting pre-dispatching.
- AEEventHandlerUPP fPreDispatchUPP;
- #endif
- OSLAccessorUPP fAccessorUPP;
- OSLCompareUPP fCompareUPP;
- OSLCountUPP fCountUPP;
- OSLDisposeTokenUPP fDisposeTokenUPP;
- OSLGetMarkTokenUPP fGetMarkTokenUPP;
- OSLMarkUPP fMarkUPP;
- OSLAdjustMarksUPP fAdjustMarksUPP;
- };
-
-
- //----------------------------------------------------------------------------------------
- // TScriptableObjectList
- //----------------------------------------------------------------------------------------
-
- const DescType cScriptableObjectList = 'olst';
-
- class TScriptableObjectList : public TList, public MScriptableObject
- {
-
- MA_DECLARE_CLASS;
-
- // This represents a list of objects that are marked by the OSL. When the
- // OSL gets a complex object specifier it determines which objects should
- // be marked for action. The marked objects are kept in a TScriptableObjectList.
-
- public:
-
- MScriptableObject* fContainer; // The object that contains the objects in this list.
-
- TScriptableObjectList();
- virtual ~TScriptableObjectList();
- // Destructor
-
- virtual void IScriptableObjectList(MScriptableObject* itsContainer);
-
- virtual void DoAECountElements(TAppleEvent* message,
- TAppleEvent* reply);
-
- virtual void DoScriptCommand(CommandNumber aCommandNumber,
- TAppleEvent* message,
- TAppleEvent* reply);
- // Calls DoScriptCommand for each object in the list.
-
- virtual void InsertMarkedObject(MScriptableObject* theObjectMarked);
- // Adds an object to be marked.
-
- virtual void AdjustMarks(long newStart,
- long newStop);
- // Changes the range of objects to be marked.
-
- virtual MScriptableObject* GetObjectsContainer();
- // Returns fContainer.
-
- virtual Boolean IsPropertyList();
- // Is this a list of property descriptors?
-
- virtual MScriptableObject* GetContainedObject(DescType desiredType,
- DescType selectionForm,
- const CAEDesc& selectionData);
- // Returns a list of objects contained within the objects in this list.
-
- virtual long CountContainedObjects(DescType desiredType);
- // Adds up CountContainedObjects for all objects in the list.
-
- };
-
- //----------------------------------------------------------------------------------------
- // TSetPropertyCommand
- //----------------------------------------------------------------------------------------
-
- #ifndef __UCOMMAND__
- #include "UCommand.h"
- #endif
-
- class TSetPropertyCommand : public TCommand
- {
-
- MA_DECLARE_CLASS;
-
- // When MacApp recieves a SetData Apple Event for a property it will create a
- // TSetPropertyCommand to actually do the property setting. It will call the
- // object whose property is being set to get specifics for the command.
- // If you want to provide your own command number so the undo menu looks right,
- // override the object's GetSetPropertyInfo method to supply your own command
- // information.
- // TSetPropertyCommand implements undo by first calling GetObjectProperty for
- // each affected object, then restoring this value on undo.
-
- public:
-
- TList* fObjectsToSet;
- TList* fObjectsToFree;
- DescType fProperty;
- CAEDesc fOldPropData;
- CAEDesc fNewPropData;
-
- TSetPropertyCommand();
-
- virtual void ISetPropertyCommand(DescType theProperty,
- TList* theObjectsToSet,
- // Caller must create this list. ISetPropertyCommand clones it so you can free it immediately.
- TAppleEvent* message,
- TAppleEvent* reply);
-
- virtual ~TSetPropertyCommand();
-
- virtual void DoIt();
-
- virtual void UndoIt();
-
- virtual void RedoIt();
- };
-
- //----------------------------------------------------------------------------------------
- // TPropertyAccessor
- //----------------------------------------------------------------------------------------
-
- class TPropertyAccessor : public TObject, public MScriptableObject
- {
-
- MA_DECLARE_CLASS;
-
- // When an object specifier describes a property of an object instead of the object itself,
- // MScriptableObject:GetContainedObject will create a TPropertyAccessor.
- // The TPropertyAccessor knows which property of which object are involved and can get and set the value.
-
- public:
- MScriptableObject* fWhichObject; // The object whose property we are concerned with.
- DescType fWhichProperty; // The property we're going to get or set.
-
- TPropertyAccessor();
- // Empty constructor to satisfy compiler.
- virtual ~TPropertyAccessor();
- // Destructor
-
- virtual void IPropertyAccessor(MScriptableObject* whichObject,
- DescType whichProperty);
-
- virtual TCommandHandler* GetCommandContext(const CommandNumber aCommandNumber) const;
-
- virtual void DoScriptCommand(CommandNumber aCommandNumber,
- TAppleEvent* message,
- TAppleEvent* reply);
- // Handles GetData and SetData events.
-
- virtual Boolean CompareScriptableObjects(DescType operation,
- const CAEDesc& thingToCompare);
- // Compares the values of the two properties.
-
- virtual void SetObjectProperty(const CAEDesc& thePropertyValue,
- DescType whichProperty);
- // Calls SetObjectProperty for its object.
-
- virtual void DoAEGetDataSize(TAppleEvent* message,
- TAppleEvent* reply);
- // Returns the data size for its property.
-
- virtual Boolean GetObjectProperty(CAEDesc& thePropertyValue,
- DescType whichProperty,
- const CAEDesc& desiredType);
- // Calls GetObjectProperty for its object.
-
- #if qAttachable
- virtual Boolean HandleOSAEvent(CommandNumber aCommandNumber,
- TAppleEvent* message,
- TAppleEvent* reply);
- #endif
- };
-
- //----------------------------------------------------------------------------------------
- // TSetPropertyEvent
- //----------------------------------------------------------------------------------------
-
- class TSetPropertyEvent : public TAppleEvent
- {
-
- MA_DECLARE_CLASS;
-
- // This is a variation on TAppleEvent that handles the overhead in creating an
- // event used to change an object's property. When you want to send yourself
- // an Apple Event to set a property, use one of these.
-
- public:
-
- TSetPropertyEvent();
- virtual ~TSetPropertyEvent();
- // Destructor
-
- virtual void ISetPropertyEvent(const CAEDesc& itsAddress,
- long itsSendingMode,
- MScriptableObject* objectToSet,
- DescType propType);
- // Initializes the event with the recipient, sending mode, the object you want to set and the
- // property you want to modify. Creates an object specifier for the object's property and
- // sticks it in the direct object.
- };
-
- //----------------------------------------------------------------------------------------
- // TMenuAccessor
- //----------------------------------------------------------------------------------------
-
- class TMenuAccessor : public TObject, public MScriptableObject
- {
-
- MA_DECLARE_CLASS;
-
- // When an object specifier describes a menu, TApplication:GetContainedObject will
- // create a TMenuAccessor.
- // The TMenuAccessor knows which menu is targeted and can act on events directed at the menu.
-
- public:
- MenuRef fMenu; // The menu we handle events for.
- short fIndex; // Index of the menu in the displayed menu bar.
-
- TMenuAccessor();
- virtual ~TMenuAccessor();
- // Destructor
-
- virtual void IMenuAccessor(MenuRef theMenu, short theIndex);
-
- virtual Boolean GetObjectProperty(CAEDesc& thePropertyValue,
- DescType whichProperty,
- const CAEDesc& desiredType);
- // Returns properties for the menu.
-
- virtual MScriptableObject* GetIndContainedObject(DescType desiredType,
- long index);
- // Returns a menu item contained within a menu.
-
- virtual long CountContainedObjects(DescType desiredType);
- // Returns the number of menu item in a menu.
-
- virtual void SetObjectProperty(const CAEDesc& thePropertyValue,
- DescType whichProperty);
- };
-
- //----------------------------------------------------------------------------------------
- // TMenuItemAccessor
- //----------------------------------------------------------------------------------------
-
- class TMenuItemAccessor : public TObject, public MScriptableObject
- {
-
- MA_DECLARE_CLASS;
-
- // When an object specifier describes a menuitem, TMenuAccessor:GetContainedObject will
- // create a TMenuItemAccessor.
- // The TMenuItemAccessor knows which item is targeted and can act on events
- // directed at that item.
-
- public:
- MenuRef fMenu; // The menu the item is in.
- short fMenuItem; // Index of the menu item.
-
- TMenuItemAccessor();
- virtual ~TMenuItemAccessor();
- // Destructor
-
- virtual void IMenuItemAccessor(MenuRef theMenu, short theItem);
-
- virtual Boolean GetObjectProperty(CAEDesc& thePropertyValue,
- DescType whichProperty,
- const CAEDesc& desiredType);
- // Returns properties for the menu item.
-
- virtual void DoAEOpen(TAppleEvent* message, TAppleEvent* reply);
- // Simulates choosing the menu item
-
- virtual void SetObjectProperty(const CAEDesc& thePropertyValue,
- DescType whichProperty);
- };
-
- //------------------------------------------------------------------------------------
- // TOSADispatcher Inlines
- //------------------------------------------------------------------------------------
-
- inline void TOSADispatcher::SetDefaultTarget(MDefaultScriptableObject* defaultTarget)
- {
- fDefaultTarget = defaultTarget;
- };
-
- inline MDefaultScriptableObject* TOSADispatcher::GetDefaultTarget()
- {
- return fDefaultTarget;
- };
-
- inline void TOSADispatcher::SetAEResolveFlags(const short newFlags)
- {
- fAEResolveFlags = newFlags;
- };
-
- //------------------------------------------------------------------------------------
- // Global Functions
- //------------------------------------------------------------------------------------
-
- // Initialize the scripting unit. Call this function to create a global
- // instance of TScriptEventDispatcher, and to install events supported in
- // Apple Event dispatch table resources.
-
- inline void InitUScripting()
- {
- MA_REGISTER_CLASS(TOSADispatcher);
- TOSADispatcher::InitUScripting(&TOSADispatcher::fgClassDesc);
- }
-
- // To initialize scripting with a custom scripting dispatcher, use
- // some code like this:
- //
- // MA_REGISTER_CLASS(TMyCustomOSADispatcher);
- // TMyCustomOSADispatcher::InitUScripting(&TMyCustomOSADispatcher::fgClassDesc);
-
- #endif // __USCRIPTING__
-